package com.distribute.portal.dao.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.distribute.portal.dao.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Tuple;

public class JedisClientSingle implements JedisClient {

	@Autowired
	private JedisPool jedisPool;

	@Override
	public String get(String key) {
		Jedis jedis = jedisPool.getResource();
		String string = jedis.get(key);
		jedis.close();
		return string;
	}

	@Override
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String string = jedis.set(key, value);
		jedis.close();
		return string;
	}

	@Override
	public String hget(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		String string = jedis.hget(hkey, key);
		jedis.close();
		return string;
	}

	@Override
	public long hset(String hkey, String key, String value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hset(hkey, key, value);
		jedis.close();
		return result;
	}

	@Override
	public long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.incr(key);
		jedis.close();
		return result;
	}

	@Override
	public long expire(String key, int second) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.expire(key, second);
		jedis.close();
		return result;
	}

	@Override
	public long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.ttl(key);
		jedis.close();
		return result;
	}

	@Override
	public long del(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.del(key);
		jedis.close();
		return result;
	}

	@Override
	public long hdel(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hdel(hkey, key);
		jedis.close();
		return result;
	}

	@Override
	public Long zadd(String key, double score, String member) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.zadd(key, score, member);
		jedis.close();
		return result;
	}

	//返回sortedset中指定范围的member,默认为升序1。2.3  若end为负数，则表示从后数起的第几个数。如start=0,end=-1.表示取全部member
	@Override
	public Set<String> zrange(String key, long start, long end) {
		Jedis jedis = jedisPool.getResource();
		Set<String> result = jedis.zrange(key, start, end);
		jedis.close();
		return result;
	}

	@Override
	public Set<String> zrevrange(String key, long start, long end) {
		Jedis jedis = jedisPool.getResource();
		Set<String> result = jedis.zrevrange(key, start, end);
		return result;
	}

	//对指定key的sortedset中的member的score进行操作，参数score:增加score>0,减少score<0
	@Override
	public Double zincrby(String key, double score, String member) {
		Jedis jedis = jedisPool.getResource();
		Double result = jedis.zincrby(key, score, member);
		return result;
	}
	//通过索引区间返回有序集合成指定区间内的成员,score也返回     new Tuple(element,score);升序
	@Override
	public Set<Tuple> zrangeWithScores(String key, long start, long end) {
		Jedis jedis = jedisPool.getResource();
		Set<Tuple> result = jedis.zrangeWithScores(key, start, end);
		return result;
	}

	//降序
	@Override
	public Set<Tuple> zrevrangeWithScores(String key, long start, long end) {
		Jedis jedis = jedisPool.getResource();
		Set<Tuple> result = jedis.zrangeWithScores(key, start, end);
		return result;
	}

}
